package com.cskaoyan.config;

/**
 * 对应前端接受的4中操作类型和响应值
 */

public enum ActionType {
    COMMON("read:list", 0),//一般操作
    SAFE("create", 1),//安全操作
    ORDER("order", 2),//订单操作
    OTHERS("", 3);//其他操作
    private int code;
    private String action;

    ActionType(String action, int code) {
        this.code = code;
        this.action = action;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    /**
     * 根据 admin:auth:login 格式的动作 返回设定的 操作类型
     * @param action
     * @return
     */
    public static Integer getType(String action) {
        if (containsAction(action, ORDER.action)) {
            return ORDER.code;
        }
        if (containsAction(action, COMMON.action)) {
            return COMMON.code;
        }
        if (containsAction(action, SAFE.action)) {
            return SAFE.code;
        }
        return OTHERS.code;
    }
    //判断是否包含设定的操作类别
    private static boolean containsAction(String action, String actionTypes) {
        String[] split = actionTypes.split(":");
        for (String s : split) {
            if (action.contains(s)) {
                return true;
            }
        }
        return false;
    }

}
